<?php
namespace app\admin\controller;

use app\admin\model\User as UserModel;
use think\Request;
use think\Controller;
use app\admin\model\Account;
use app\admin\model\Address;
use think\Db;

  class user extends Controller
  {

  
   //会员列表展示管理
    public function admin_role() 
    {
       // $result = Db::name('user')->where('udertype',1);
       // dump($result);
      //exit;
       $list = UserModel::where('udertype',0)->select();
      // var_dump($list);
       $count = count($list);
       $this->assign("count",$count);

       $user = UserModel::where('udertype',0)->paginate(4);
       $this->assign("user",$user);
       return $this->fetch();

    }

   //点击删除会员所有信息
     public function delete()
    {
      $request = Request::instance();
        if($request->isAjax()){ 
            $gid = input('mid');
           UserModel::where('id',$gid)->delete(); 
           Db::name('account')->where('user_id',$gid)->delete();
           Db::name('address')->where('user_id',$gid)->delete();
           //dump($result);
           //$this->redirect('admin_role');   
           //echo $gid;
      }    
     
   }

   //选择删除会员所有信息
    public function deletes()
    {
        if(empty($_POST['like'])){
           $this->redirect('admin_role');
          //return '请选择要删除的用户';
          //$this->success('请选择要删除的用户','/admin/user/admin_role');
        }    
       $topic = $_POST['like'];
        foreach ($topic as $deletetopic) {
          UserModel::where('id',$deletetopic)->delete();  
          Db::name('account')->where('user_id',$deletetopic)->delete(); 
          Db::name('address')->where('user_id',$deletetopic)->delete();
        } 
          $this->redirect('admin_role');
    }
 
   //会员等级信息展示管理
    public function admin_grade()
    {
       //$list = UserModel::where('udertype',0)->select();
       $list = Db::name('user')->where('udertype',0)->select();
        //$list =toArray($list);
        //var_dump($list);
        //exit;
       $count = count($list);
       $this->assign("list",$list);
       $this->assign("count",$count);
       $result = Db::name('account')->paginate(4);
       $this->assign("result",$result);
       //dump($result);
        //exit;
      return $this->fetch();
    }
   
   //会员等级信息更改
    public function  grade(){
       $update = new Account;
       $update->save([
                        //'user_id'        =>  input('post.user_id'),
                        'lever'    =>  input('post.lever'),
                        'score'    =>  input('post.score'),
                        'balance'  =>  input('post.balance'),
                        //'ablanve'   =>  input('post.address')
                    ],['id' => input('post.id')]);

      $this->redirect('admin_grade');
    }

    //会员地址信息展示管理
    public function admin_list()
    {  
      $list = Db::name('address')->select();
      $count = count($list);
      $this->assign("count",$count);
     //查询用户地址等信息按每页4条数据展示
     $result = Db::name('address')->paginate(4); 
     /*var_dump($result);
     exit;
     foreach ($result as $key => $value) {
            return $value;
        }
       var_dump($value);*/
  
     /* foreach ($result as $v) {
          return $v;
          var_dump($v);
          exit();
        } 
     var_dump($v[0].username);
     exit();*/
     $this->assign("result",$result);   
    return $this->fetch(); 
    }
    
   //会员地址信息更改
    public function  list(){
     /* var_dump($_POST);
      exit();
      return $this->fetch(); */
       $update = new Address;
       $update->save([
                        //'user_id' =>  input('post.user_id'),
                          'name'    =>  input('post.name'),
                          'city'    =>  input('post.city'),
                          'address' =>  input('post.address'),
                          'phone'   =>  input('post.phone'),
                        //'ablanve'   =>  input('post.address')
                    ],['id' => input('post.id')]);
      $this->redirect('admin_list');
    }

  }